#ifndef __HTTP_CLOUD__
#define __HTTP_CLOUD__

#include <QObject>
#include <QThread>

#include "common.h"

#define HTTP_LOGIN_REQUEST				"/sys/login/device"
#define HTTP_ADD_TRANSACTION_REQUEST	"/api/depositRecord"
#define HTTP_REPORT_DEVICE_REQUEST		"/api/deviceStatus"

class Coordinator;
class QTimer;

class HttpCloud_Worker : public QObject
{
	Q_OBJECT

public:

	HttpCloud_Worker(Coordinator *coordinator, QObject *parent = nullptr);
	~HttpCloud_Worker();

public:

    bool online() { return m_online; };

signals:

	void transaction_uploaded(TransInfo trans_info);
	void sw_upgrade(QString link, QString file_name, QString new_version);

public slots:

	void start_work();
	void stop_work();

	bool upload_transaction(TransInfo trans_info);

private slots:

	void heartbreak();
	void upgrade_check();

private:

	Coordinator *m_coordinator;

    QString m_server_address;
    QString m_auth_code;
    bool m_online;
    uint32_t m_heartbreak_period;
    uint32_t m_transfer_timeout;

	QTimer *m_heartbreak_timer;
	QTimer *m_upgrade_timer;

	static QByteArray s_aes_private_key;

public:

	static QByteArray aes_encrypt(QByteArray ba);
	static QByteArray aes_decrypt(QByteArray ba);

};

class HttpCloud : public QObject
{
	Q_OBJECT

public:

    HttpCloud(QObject *parent = nullptr);
    ~HttpCloud();

    HttpCloud_Worker *getWorker() {
        return m_worker;
    }

signals:

	void start();
	void stop();

private:

	QThread m_worker_thread;
    HttpCloud_Worker *m_worker;

    Coordinator *m_coordinator;
};


#endif
